import 'dart:async';

import 'package:flutter/material.dart';
import 'package:healthy_station_flutter/utils/res_images.dart';
import 'package:provider/provider.dart';

import '../../provider/provider_login.dart';
import '../../utils/constants.dart';
import '../../utils/res_colors.dart';
import '../../utils/res_strings.dart';
import '../../utils/screen_adapter.dart';

/// 
/// @Description 
/// @Author WuJianFeng
/// @Date 2024/3/4 14:12
/// 

class LoginSocialWidget extends StatefulWidget{
  const LoginSocialWidget({super.key});

  @override
  State<StatefulWidget> createState() => _LoginSocialWidgetState();
  
}

class _LoginSocialWidgetState extends State<LoginSocialWidget> with SingleTickerProviderStateMixin{

  var currentTime = 60;
  late Timer myTimer;

  @override
  void initState() {
    anim();
    timer();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Container(
          margin: EdgeInsets.only(top: Adapt.pt(56)),
          width: Adapt.pt(254),
          height: Adapt.pt(362),
          child: ImageUtils.getImg("login_content_social.webp"),
        ),
        Container(
          margin: EdgeInsets.only(top: Adapt.pt(61)),
          child: timeRow(),
        )
      ],
    );
  }

  Widget timeRow(){
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      mainAxisSize: MainAxisSize.max,
      children: [
        RotationTransition(turns: _controller, child: Image.asset("assets/images/icon_loading.webp")),

        Container(
          margin: EdgeInsets.only(left: Adapt.pt(15), right: Adapt.pt(12)),
          child: Text(sLoginSocialTimeTip, style: TextStyle(color: Colors.white, fontSize: Adapt.pt(35))),
        ),
        Consumer<LoginProvider>(
          builder: (context, LoginProvider loginProvider, child) {
            return Text("${currentTime}秒", style: TextStyle(color: color_ff421e, fontSize: Adapt.pt(32)));
          },
        ),
      ],
    );
  }

  /// 会重复播放的控制器
  late final AnimationController _controller;

  void timer(){
    myTimer = Timer.periodic(const Duration(milliseconds: 1000), (timer) {

      if(currentTime <= 0){
        setState(() {
          timer.cancel();
          Provider.of<LoginProvider>(context,listen: false).setLoginType(loginTypeInit);
        });

      }else{

        setState(() {
          currentTime --;
        });
      }
    });
  }


  void anim(){
    _controller = AnimationController(duration: const Duration(seconds: 2), vsync: this)
      ..repeat();
  }

  @override
  void dispose() {
    myTimer.cancel();
    _controller.dispose();
    super.dispose();
  }
  
}
 
 